package kr.poturns.blink.internal;

import kr.poturns.blink.internal.comm.BluetoothDeviceX;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter.LeScanCallback;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * 디바이스 간의 통신을 담당하는 모듈. 
 * BroadcastReceiver로서 필요한 Broadcast를 필터링한다. 
 * 블루투스 커뮤니케이션을 관리하고 조절한다 ({@link BluetoothAssistant}).
 * 
 * @author Yeonho.Kim
 * @since 2014.07.12
 *
 */
class InterDeviceManager extends BroadcastReceiver implements LeScanCallback {

	// *** CONSTANT DECLARATION *** //
	/**
	 * BroadcastReceiver Intent Action명 
	 */
	public static final String ACTION_NAME = "kr.poturns.blink.internal.action.InterDeviceManager";
	
	
	// *** STATIC DECLARATION *** //
	/**
	 * InterDeviceManager의 Singleton-인스턴스
	 */
	private static InterDeviceManager sInstance = null;
	
	/**
	 * InterDeviceManager의 Singleton-인스턴스를 반환한다. 
	 *  
	 * @param context ( :{@link BlinkBaseService} )
	 * @return context가 Null일 경우, 기존의 Instance를 반환한다.
	 */
	static InterDeviceManager getInstance(Context context) {
		if (sInstance == null && context != null)
			sInstance = new InterDeviceManager(context);
		return sInstance;
	}
	

	// *** FIELD DECLARATION *** //
	private final Context MANAGER_CONTEXT;
	
	private InterDeviceManager(Context context) {
		this.MANAGER_CONTEXT = context;
		
		init();
	}
	
	void init() {
		MANAGER_CONTEXT.registerReceiver(this, BluetoothAssistant.obtainIntentFilter());
		
		BluetoothAssistant mAssistant = BluetoothAssistant.getInstance(this);
		mAssistant.init();
	}
	
	void destroy() {
		MANAGER_CONTEXT.unregisterReceiver(this);
		
		sInstance = null;
		System.gc();
	}

	
	// *** CALLBACK DECLARATION *** //
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		
		if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
			int curr_state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
			int prev_state = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, BluetoothAdapter.ERROR);
			
		} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
			
			if (mInterDeviceListener != null) {
				
			}
			
		} else if (BluetoothAssistant.ACTION_STATE_ON.equals(action)) {
			BluetoothAssistant.getInstance(this).startClassicServer();
		}
	}

	@Override
	public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
		// TODO Auto-generated method stub
		
		if (mInterDeviceListener != null) {
			
		}
	}
	
	private InterDeviceEventListener mInterDeviceListener = null;

	public interface InterDeviceEventListener {
		//public void onAlertDialog(int key, Runnable accept, Runnable cancel);
		//public void onInformDialog(int key);
		
		public void onDeviceDiscovered(BluetoothDeviceX deviceX);
		
		public void onDeviceConnected(BluetoothDeviceX deviceX);
		
		public void onDeviceDisconnected(BluetoothDeviceX deviceX);
		
		public void onMessageReceivedFrom(String message, BluetoothDeviceX deviceX);
	}

	public InterDeviceEventListener getInterDeviceListener() {
		return mInterDeviceListener;
	}
	
	public void setInterDeviceListener(InterDeviceEventListener mInterDeviceListener) {
		this.mInterDeviceListener = mInterDeviceListener;
	}
	
	Context getContext() {
		return MANAGER_CONTEXT;
	}
}
